package org.geektimes.projects.userweb.service;

import org.geektimes.projects.userweb.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2021/5/4 22:46
 */
@Service
public class UserServiceImpl implements UserService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());




    private static List<org.geektimes.projects.userweb.model.User> users = new ArrayList<>();

    static {
        users.add(new org.geektimes.projects.userweb.model.User(1L, "张三", 18));
        users.add(new org.geektimes.projects.userweb.model.User(2L, "李四", 28));
        users.add(new org.geektimes.projects.userweb.model.User(3L, "王五", 38));
    }


    @Override
    @Cacheable(cacheNames = "cache_user", key = "'user_'+ #id", unless = "#result == null")
    public User getUser(Long id) {
        org.geektimes.projects.userweb.model.User user = users.stream().filter(u -> u.getId().equals(id)).findFirst().orElse(null);
        logger.info("{} 查询的结果为 ：{}", id, user == null ? "null" : user.toString());

        return user;
    }

    @Override
    @CacheEvict(cacheNames = "cache_user", key = "'user_'+ #id")
    public String delUser(Long id) {
        return "执行成功";
    }




}
